﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using XiAnAirport.BUS.Escalator;
using XiAnAirport.DAL;
using XiAnAirport.Models;

namespace XiAnAirport.BUS
{
    public class EscalatorBUS
    {
        public static List<EscalatorInfo> list = new List<EscalatorInfo>();
        public static List<EscalatorSocket> device_list = new List<EscalatorSocket>();

        public EscalatorResponseMessage GetAllDataByTime(string datetTime, int elevatorid, string partName)
        {
            EscalatorResponseMessage response = new EscalatorResponseMessage();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetFileNameByTime?datetTime={1}&partName={2}", elevator.ipAddress, datetTime, partName));
                response = JsonConvert.DeserializeObject<EscalatorResponseMessage>(re);
            }
            catch
            {

            }
            return response;
        }
        public EscalatorResponseMessage2 GetAllFileNameByTimeRange(string startTime, string endTime, int elevatorid, string partName)
        {
            EscalatorResponseMessage2 response = new EscalatorResponseMessage2();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetAllFileNameByTimeRange?startTime={1}&endTime={2}&partName={3}", elevator.ipAddress, startTime,endTime, partName));
                response = JsonConvert.DeserializeObject<EscalatorResponseMessage2>(re);
            }
            catch
            {

            }
            return response;
        }
        


        public string HttpClientGet(string url)
        {
            var httpClientHandler = new HttpClientHandler();
            httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
            HttpClient httpClient = new HttpClient(httpClientHandler);//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //请求
            HttpResponseMessage response = httpClient.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }

        public DeviceSnsResponseMessage GetDeviceSns(int elevatorid)
        {
            DeviceSnsResponseMessage response = new DeviceSnsResponseMessage();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetDeviceSns", elevator.ipAddress));
                response = JsonConvert.DeserializeObject<DeviceSnsResponseMessage>(re);
            }
            catch
            {

            }
            return response;
        }

        public ConfigParamModelResponseMessage GetAlarmAndWarn(int elevatorid, string device_sn)
        {
            ConfigParamModelResponseMessage response = new ConfigParamModelResponseMessage();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetAlarmAndWarn?device_sn={1}", elevator.ipAddress,device_sn));
                response = JsonConvert.DeserializeObject<ConfigParamModelResponseMessage>(re);
            }
            catch
            {

            }
            return response;
        }

        public WarnsResponseMessage GetWarns(int elevatorid)
        {
            WarnsResponseMessage response = new WarnsResponseMessage();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetWarns", elevator.ipAddress));
                response = JsonConvert.DeserializeObject<WarnsResponseMessage>(re);
            }
            catch
            {

            }
            return response;
        }

        public WarnsResponseMessage GetAlarms(int elevatorid)
        {
            WarnsResponseMessage response = new WarnsResponseMessage();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetAlarms", elevator.ipAddress));
                response = JsonConvert.DeserializeObject<WarnsResponseMessage>(re);
            }
            catch
            {

            }
            return response;
        }

        public ConfigModelResponseMessage GetDeviceListNames(int elevatorid, string deviceName)
        {
            ConfigModelResponseMessage response = new ConfigModelResponseMessage();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetDeviceListNames?deviceNames={1}", elevator.ipAddress,deviceName));
                response = JsonConvert.DeserializeObject<ConfigModelResponseMessage>(re);
            }
            catch
            {

            }
            return response;
        }

        public ChannelDataResponseMessage GetDatasByFileName(int elevatorid, string path)
        {
            ChannelDataResponseMessage response = new ChannelDataResponseMessage();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetDatasByFileName?path={1}", elevator.ipAddress, path));
                response = JsonConvert.DeserializeObject<ChannelDataResponseMessage>(re);
            }
            catch
            {

            }
            return response;
        }

        public ConfigModelResponseMessage GetPartNames(int elevatorid)
        {
            ConfigModelResponseMessage response = new ConfigModelResponseMessage();
            try
            {
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                Elevator elevator = elevatorAreaDAL.LoadElevator(elevatorid);
                string re = HttpClientGet(string.Format("http://{0}/api/Common/GetPartNames", elevator.ipAddress));
                response = JsonConvert.DeserializeObject<ConfigModelResponseMessage>(re);
            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = ex.Message;
            }
            return response;
        }
    }
}
